package com.xmkj.jijidriver.order;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.carbean.OderListXcBean;
import com.common.cons.Constans;
import com.common.jiji.entity.BaseJJBean;
import com.common.jiji.methods.DateOrderMethods;
import com.common.jiji.parms.OrderDellistParms;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.XiCheOrderListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.jijidriver.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/12/19.
 */
@SuppressLint("ValidFragment")
public class OrderFragment extends BaseMvpFragment {


    int position;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.tv_shouru)
    TextView mTvShouru;
    @BindView(R.id.tv_bishu)
    TextView mTvBishu;

    private List<OderListXcBean.ListsEntity> bean = new ArrayList<>();
    private OrderDetailsAdapter adapter;


    public OrderFragment(int position) {
        this.position = position;
    }

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected void onViewCreated(View view) {
        recyclerview =view.findViewById(R.id.recyclerview);
        mTvDate =view.findViewById(R.id.tv_date);
        mTvShouru =view.findViewById(R.id.tv_shouru);
        mTvBishu =view.findViewById(R.id.tv_bishu);

    }

    @Override
    protected void doLogicFunc() {
        gotoHttpRep();
        setRecyclerView();
        switch (position){
            case 1:
                mTvDate.setText("当日收入");
                break;
            case 2:
                mTvDate.setText("昨日收入");
                break;
            case 3:
                mTvDate.setText("当月收入");
                break;
            case 4:
                mTvDate.setText("上月收入");
                break;

        }
    }

    @Override
    protected void onViewClicked(View view) {
        super.onViewClicked(view);

    }


    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }

    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        adapter = new OrderDetailsAdapter(context, bean, position);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }



    private void gotoHttpRep() {
        CommonSubscriber<BaseJJBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
               /* OderListXcBean beans = (OderListXcBean) o;
                mTvShouru.setText(beans.stotal + "");
                mTvBishu.setText("共"+beans.lists.size() + "笔交易");
                List<OderListXcBean.ListsEntity> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }*/
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });

        OrderDellistParms orderDellistParms = new OrderDellistParms();
        orderDellistParms.dateType = position;
        orderDellistParms.pageIndex = mPageIndex;
        orderDellistParms.pageSize = mPageSize;
        DateOrderMethods.getInstance().getOrderListByDate(subscriber,orderDellistParms );
        rxManager.add(subscriber);

    }



}
